package jh.jianguan.springboot.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @description: jedis配置类
 * @author: guoc
 * @createDate: 2020/12/30 11:30
 */
@Configuration
@EnableAutoConfiguration
@EnableCaching
@Slf4j
public class JedisConfig implements InitializingBean {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    @Value("${spring.redis.jedis.pool.max-active}")
    private int maxTotal;

    @Value("${spring.redis.jedis.pool.max-idle}")
    private int maxIdle;

    @Value("${spring.redis.jedis.pool.min-idle}")
    private int minIdle;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.timeout}")
    private String timeout;


    /**
     * 初始化jedis连接池
     * @return redis.clients.jedis.ShardedJedisPool
     * @author guoc
     * @date 2020/12/30 11:39
     */
    @Bean
    public JedisPool jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(maxTotal);
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMinIdle(minIdle);
        String timeString = timeout.substring(0, timeout.indexOf("m"));
        int time = Integer.parseInt(timeString);
        return new JedisPool(jedisPoolConfig, host, port, time, password);
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("****************正在启动Redis****************");
        log.info("******Redis-Url:" + host + ":" + port + "******");
        log.info("********************************************");
    }
}
